package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    /**
     * 添加购物车
     */
    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        //判读商品在当前购物车中是否已经存在
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        //若商品已经存在，只需要数量加一（即更新update操作而不是插入insert操作）
        if (list != null && list.size() > 0) {
            //要么没有返回结果，有返回结果的话只可能有一条符合条件
            ShoppingCart cart = list.get(0);
            cart.setNumber(cart.getNumber() + 1);//update shopping_cart set number=?where id=?
            shoppingCartMapper.updateNumberById(cart);
        } else {
            //若商品不存在，则添加一条商品数据到购物车
            //判断是菜品还是套餐，且只可能满足一个
            Long dishId = shoppingCartDTO.getDishId();
            if (dishId != null) {
                //添加到购物车的是菜品
                Dish dish = dishMapper.getById(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            } else {
                Long setmealId = shoppingCartDTO.getSetmealId();

                Setmeal setmeal = setmealMapper.getById(setmealId);
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);
        }
    }

    /**
     *查看购物车
     */
    @Override
    public List<ShoppingCart> showShoppingCart() {
        //获取当前微信用户的Id
        Long userId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart=ShoppingCart.builder()
                        .userId(userId)
                        .build();
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        return list;
    }
    /**
     * 删除购物车
     */
    @Override
    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
            //对象拷贝封装请求参数
            ShoppingCart shoppingCart = new ShoppingCart();
            BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
            //设置查询条件，查询当前登录用户的购物车数据
            shoppingCart.setUserId(BaseContext.getCurrentId());
            //此时根据用户id，菜品id（可能要口味数据）或者套餐id查询的结果要么是1条要么是没有
            //   因为购物车添加相同的商品只是修改数量number字段，而不是真正添加一条数据。
            List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);

            if(list != null && list.size() > 0){
                //不为空说明购物车有数据，因为只有1条所以直接获取第一个下标即可获得对象数据
                shoppingCart = list.get(0);

                //获得购物车实体类的数量字段：
                //   1.如果数量为1,说明此时只有一条数据，此时在减去1就是删除数据库的这条数据.
                //   2.如果数量不是1,说明此时有多条数据，多条数据只是number发生了变化实际上还是一条数据，所以此时的
                //     删除只需要减少number数量字段即可。
                Integer number = shoppingCart.getNumber();
                if(number == 1){
                    //当前商品在购物车中的份数为1，直接删除当前记录
                    shoppingCartMapper.deleteById(shoppingCart.getId());
                }else {
                    //当前商品在购物车中的份数不为1，修改份数即可
                    shoppingCart.setNumber(shoppingCart.getNumber() - 1);
                    shoppingCartMapper.updateNumberById(shoppingCart);
                }
            }

    }

    /**
     * 清空购物车
     */
    @Override
    public void cleanShoppingCart() {
        //获取到当前用户的id
        Long userId = BaseContext.getCurrentId();
        shoppingCartMapper.deleteByUserId(userId);
    }
}
